nanopyx.methods.drift_alignment.corrector

 1from .estimator_table import DriftEstimatorTable
 2from ...core.utils.timeit import timeit
 3from ...core.transform import translation
 4
 5import cv2
 6import numpy as np
 7from skimage.transform import EuclideanTransform, warp
 8
 9
10class DriftCorrector(object):
11    """
12    Main class for aligning timelapse images with drift.
13    Required previous calculation of a drift table.
14    Implements the following methods:
15    - apply_correction
16    - load_drift_table
17    - _translate_slice
18    """
19    def __init__(self):
20        self.estimator_table = DriftEstimatorTable()
21        self.image_arr = None
22
23    def _translate_slice(self, slice_idx):
24        """
25        Method used to translate individual slices.
26        Requires previous loading of the timelapse image array with shape (n_slices, rows, columns) and drift table.
27        Takes a single index and calculates the translated slice for that index.
28        :param slice_idx: int corresponding to the slice to be translated
29        :return: translated image slice
30        """
31        drift_x = self.estimator_table.drift_table[slice_idx][1]
32        drift_y = self.estimator_table.drift_table[slice_idx][2]
33
34        if drift_x == 0 and drift_y == 0:
35            return self.image_arr[slice_idx]
36        else:
37            return cv2.warpAffine(self.image_arr[slice_idx].astype(np.float32), np.float32([[1, 0, drift_x], [0, 1, drift_y]]), self.image_arr[slice_idx].shape[:2][::-1]).astype(self.image_arr.dtype)
38
39    # @timeit
40    def apply_correction(self, image_array):
41        """
42        Main method of DriftCorrector class.
43        Translates each image slice according to the drift table.
44        :param image_array: numpy array with shape (n_slices, rows, columns)
45        :return: aligned image array with shape (n_slices, rows, columns)
46        """
47        if self.estimator_table.drift_table is not None:
48            self.image_arr = image_array
49            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
50            return np.array(corrected_image)
51            # return np.array(translation.translate_array(image_array.astype(np.float32),
52            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
53
54        else:
55            print("Missing drift calculation")
56            return None
57
58    def load_estimator_table(self, path=None):
59        """
60        Method used to load the drift table.
61        :param path: path to a .csv or .npy drift table
62        :return: None, stores drift table data in self.drift_table
63        """
64        if path is None:
65            path = input("Please provide a filepath to the drift table")
66
67        if path.split(".")[-1] == "npy":
68            self.estimator_table.import_npy(path)
69        elif path.split(".")[-1] == "csv":
70            self.estimator_table.import_csv(path)
class DriftCorrector:
11class DriftCorrector(object):
12    """
13    Main class for aligning timelapse images with drift.
14    Required previous calculation of a drift table.
15    Implements the following methods:
16    - apply_correction
17    - load_drift_table
18    - _translate_slice
19    """
20    def __init__(self):
21        self.estimator_table = DriftEstimatorTable()
22        self.image_arr = None
23
24    def _translate_slice(self, slice_idx):
25        """
26        Method used to translate individual slices.
27        Requires previous loading of the timelapse image array with shape (n_slices, rows, columns) and drift table.
28        Takes a single index and calculates the translated slice for that index.
29        :param slice_idx: int corresponding to the slice to be translated
30        :return: translated image slice
31        """
32        drift_x = self.estimator_table.drift_table[slice_idx][1]
33        drift_y = self.estimator_table.drift_table[slice_idx][2]
34
35        if drift_x == 0 and drift_y == 0:
36            return self.image_arr[slice_idx]
37        else:
38            return cv2.warpAffine(self.image_arr[slice_idx].astype(np.float32), np.float32([[1, 0, drift_x], [0, 1, drift_y]]), self.image_arr[slice_idx].shape[:2][::-1]).astype(self.image_arr.dtype)
39
40    # @timeit
41    def apply_correction(self, image_array):
42        """
43        Main method of DriftCorrector class.
44        Translates each image slice according to the drift table.
45        :param image_array: numpy array with shape (n_slices, rows, columns)
46        :return: aligned image array with shape (n_slices, rows, columns)
47        """
48        if self.estimator_table.drift_table is not None:
49            self.image_arr = image_array
50            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
51            return np.array(corrected_image)
52            # return np.array(translation.translate_array(image_array.astype(np.float32),
53            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
54
55        else:
56            print("Missing drift calculation")
57            return None
58
59    def load_estimator_table(self, path=None):
60        """
61        Method used to load the drift table.
62        :param path: path to a .csv or .npy drift table
63        :return: None, stores drift table data in self.drift_table
64        """
65        if path is None:
66            path = input("Please provide a filepath to the drift table")
67
68        if path.split(".")[-1] == "npy":
69            self.estimator_table.import_npy(path)
70        elif path.split(".")[-1] == "csv":
71            self.estimator_table.import_csv(path)

Main class for aligning timelapse images with drift. Required previous calculation of a drift table. Implements the following methods:

  • apply_correction
  • load_drift_table
  • _translate_slice
estimator_table
image_arr
def apply_correction(self, image_array):
41    def apply_correction(self, image_array):
42        """
43        Main method of DriftCorrector class.
44        Translates each image slice according to the drift table.
45        :param image_array: numpy array with shape (n_slices, rows, columns)
46        :return: aligned image array with shape (n_slices, rows, columns)
47        """
48        if self.estimator_table.drift_table is not None:
49            self.image_arr = image_array
50            corrected_image = [self._translate_slice(i).astype(np.float32) for i in range(0, image_array.shape[0])]
51            return np.array(corrected_image)
52            # return np.array(translation.translate_array(image_array.astype(np.float32),
53            #                                             np.array(self.estimator_table.drift_table).astype(np.float32)))
54
55        else:
56            print("Missing drift calculation")
57            return None

Main method of DriftCorrector class. Translates each image slice according to the drift table.

Parameters
  • image_array: numpy array with shape (n_slices, rows, columns)
Returns

aligned image array with shape (n_slices, rows, columns)

def load_estimator_table(self, path=None):
59    def load_estimator_table(self, path=None):
60        """
61        Method used to load the drift table.
62        :param path: path to a .csv or .npy drift table
63        :return: None, stores drift table data in self.drift_table
64        """
65        if path is None:
66            path = input("Please provide a filepath to the drift table")
67
68        if path.split(".")[-1] == "npy":
69            self.estimator_table.import_npy(path)
70        elif path.split(".")[-1] == "csv":
71            self.estimator_table.import_csv(path)

Method used to load the drift table.

Parameters
  • path: path to a .csv or .npy drift table
Returns

None, stores drift table data in self.drift_table